import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/banner',
    children: [{
      path: 'banner',
      name: 'banner',
      component: () => import('@/views/banner/index'),
      meta: { title: '首页标语', icon: 'el-icon-s-home', auth: true }
    }]
  },

  {
    path: '/blog',
    component: Layout,
    redirect: '/blog/bloglist',
    name: 'blog',
    meta: { title: '文章管理', icon: 'el-icon-document' },
    children: [
      {
        path: 'bloglist',
        name: 'bloglist',
        component: () => import('@/views/blog/bloglist/index'),
        meta: { title: '文章列表', icon: 'el-icon-s-order', auth: true }
      },
      {
        path: 'blogtype',
        name: 'blogtype',
        component: () => import('@/views/blog/blogtype/index'),
        meta: { title: '文章分类', icon: 'el-icon-menu', auth: true }
      },
      {
        path: 'addblog',
        name: 'addblog',
        component: () => import('@/views/blog/addblog/index'),
        meta: { title: '添加文章', icon: 'el-icon-circle-plus', auth: true }
      },
      {
        path: 'editblog/:id',
        name: 'editblog',
        component: () => import('@/views/blog/editblog/index'),
        hidden: true,
        meta: { auth: true }
      },

    ]
  },

  {
    path: '/project',
    component: Layout,
    redirect: '/project/projectlist',
    name: 'project',
    meta: { title: '项目管理', icon: 'el-icon-suitcase' },
    children: [
      {
        path: 'projectlist',
        name: 'projectlist',
        component: () => import('@/views/project/projectlist/index'),
        meta: { title: '项目列表', icon: 'el-icon-notebook-1', auth: true }
      },
      {
        path: 'addproject',
        name: 'addproject',
        component: () => import('@/views/project/addproject/index'),
        meta: { title: '添加项目', icon: 'el-icon-link', auth: true }
      }
    ]
  },

  {
    path: '/comment',
    component: Layout,
    children: [{
      path: '',
      name: 'comment',
      component: () => import('@/views/comment/index'),
      meta: { title: '评论管理', icon: 'el-icon-chat-dot-round', auth: true }
    }]
  },

  {
    path: '/message',
    component: Layout,
    children: [{
      path: '',
      name: 'message',
      component: () => import('@/views/message/index'),
      meta: { title: '留言板', icon: 'el-icon-chat-line-round', auth: true }
    }]
  },

  {
    path: '/about',
    component: Layout,
    children: [{
      path: '',
      name: 'about',
      component: () => import('@/views/about/index'),
      meta: { title: '关于我', icon: 'el-icon-user', auth: true }
    }]
  },

  {
    path: '/setting',
    component: Layout,
    children: [{
      path: '',
      name: 'setting',
      component: () => import('@/views/setting/index'),
      meta: { title: '设置', icon: 'el-icon-setting', auth: true }
    }]
  },

  {
    path: '/personalsetting',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'personalsetting',
        component: () => import('@/views/personalsetting/index'),
        meta: { title: '个人中心', icon: 'el-icon-document-copy', auth: true }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
